package cn.lm.activit.module.multi.instance;

import cn.lm.activit.module.multi.instance.cmd.AddMultiInstanceExecutionCmd;
import cn.lm.activit.module.multi.instance.cmd.DeleteMultiInstanceExecutionCmd;
import com.google.common.collect.Maps;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.testng.collections.Lists;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lm_jcf
 * @date 2018/7/13 11:09
 *
 * 并行
 */
public class MultiInstanceAssigneeTest {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    @Test
    public void testProcess() {
        // 引擎配置
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://localhost:3306/activiti-test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true")
                .setJdbcUsername("root")
                .setJdbcPassword("root")
                .setJdbcDriver("com.mysql.jdbc.Driver")
                .setDatabaseSchema("ACT")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
                .setActivityFontName("宋体")
                .setLabelFontName("宋体")
                .setDatabaseCatalog("activiti-init-data");
        // 获取流程引擎对象
        ProcessEngine processEngine = cfg.buildProcessEngine();
    }

    @Test
    public void initActivitiData() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getProcessEngineConfiguration().buildProcessEngine();

    }

    @Test
    public void deploymentProcessDefinition() {
        Deployment deployment = processEngine.getRepositoryService()    //与流程定义和部署对象相关的额Service
                .createDeployment()     //创建一个部署对象
                .name("单个任务节点多实例")
                .addClasspathResource("bpmn/multi-instance-assignee.bpmn")    //加载资源dpmn
                .deploy();   //完成部署

        System.out.println("流程Id" + deployment.getId());
        System.out.println("流程名称" + deployment.getName());
    }

    @Test
    public void startProcessInstance() {
        String processKey = "multi-instance";

        String[] assigneeList = {"a", "b", "c"};    // 发起的时候设置办理人参数

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("assigneeList", Lists.newArrayList(assigneeList));
        ProcessInstance pi = processEngine.getRuntimeService()      //与正在执行的流程实例和执行对象相关的Service
                .startProcessInstanceByKey(processKey, params);     //使用流程定义的Key启动流程实例，可以使之每次都启动最新版本的流程。

        System.out.println("流程实例ID：" + pi.getId());
        System.out.println("流程定义ID：" + pi.getProcessDefinitionId());

    }

    @Test
    public void completeMyProcessTask() {
        String taskId = "107504";

        Map<String, Object> params = new HashMap<String, Object>();
        processEngine.getTaskService()
                .complete(taskId);
        System.out.println("完成任务");
    }

    @Test
    public void deleteAssignee() {
        // 减签
        String taskId = "87523";
        String executionId = "87511";
        processEngine.getManagementService().executeCommand(new DeleteMultiInstanceExecutionCmd(executionId, true, taskId));
    }

    @Test
    public void addAssignee() {
        // 加签
        String parentExecutionId = "105005";

        String activityId = "one";

        Map<String, Object> params = Maps.newConcurrentMap();
        params.put("assignee", "e");
        processEngine.getManagementService().executeCommand(new AddMultiInstanceExecutionCmd(parentExecutionId, activityId, params));
    }

    @Test
    public void deleteProc() {

        String procInstId = "97501";
        processEngine.getRuntimeService().deleteProcessInstance(procInstId, "删除");
        processEngine.getHistoryService().deleteHistoricProcessInstance(procInstId);
    }
}
